package com.aier.service.rights.impl;


import com.aier.bean.rights.MenuNode;
import com.aier.bean.rights.Rights;
import com.aier.mapper.rights.RightsMapper;
import com.aier.service.rights.MenuNodeService;
import com.aier.service.rights.RightsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 权限表service实现类
 * */

@Service
public class RightsServiceImpl implements RightsService {

    @Autowired
    private RightsMapper rightsMapper;

    @Autowired
    private MenuNodeService menuNodeService;

    @Override
    public MenuNode getRouteMenuNodeByStaffId(String staffId) {
        List<Rights> rightsList = getNodeIdByStaffId(staffId);
        /** 创建root节点 **/
        MenuNode rootMenuNode = new MenuNode();
        rightsList.stream().forEach(rights -> {
            MenuNode menuNode = menuNodeService.getMenuNodeByNodeId(rights.getNodeId());
            List<MenuNode> menuNodeList = new ArrayList<>();
            findRootNode(menuNode, menuNodeList);
            /** 节点轨迹是子->父->爷，需反转一下 **/
            Collections.reverse(menuNodeList);
            assemblyMenuNode(rootMenuNode,menuNodeList);
        });
        return rootMenuNode;
    }

    @Override
    public List<Rights> getNodeIdByStaffId(String staffId) {
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Rights::getStaffId,staffId);
        return rightsMapper.selectList(queryWrapper);
    }

    /**
     * 递归寻找父节点
     * @param menuNode 当前节点
     * @param menuNodeList 寻找轨迹
     */
    public void findRootNode(MenuNode menuNode,List<MenuNode> menuNodeList){
        if (!menuNodeService.isExistMenuNode(menuNode,menuNodeList)){
            menuNodeList.add(menuNode);
        }

        /** 父节点不为空 **/
        if (menuNode.getFatherNodeId() != null && !menuNode.getFatherNodeId().trim().isEmpty()){
            /** 获取父节点信息 **/
            MenuNode fatherMenuNode = menuNodeService.getMenuNodeByNodeId(menuNode.getFatherNodeId());
            findRootNode(fatherMenuNode,menuNodeList);
        }
    }

    /**
     * 递归将轨迹组装到root节点中
     * @param rootMenuNode root节点
     * @param menuNodeList 轨迹
     * @return
     */
    private void assemblyMenuNode(MenuNode rootMenuNode,List<MenuNode> menuNodeList){
        /** 轨迹列表如果为空则直接返回 **/
        if (menuNodeList == null || menuNodeList.size() == 0) return;
        /** rootMenuNode子列表为null，赋值一个空list **/
        if (rootMenuNode.getChildrenNode() == null){
            rootMenuNode.setChildrenNode(new ArrayList<>());
        }
        /** 如果子列表不含有节点，则直接添加 **/
        if (!menuNodeService.isExistMenuNode(menuNodeList.get(0),rootMenuNode.getChildrenNode())){
            rootMenuNode.getChildrenNode().add(menuNodeList.get(0));
        }
        /** 轨迹要么已存在，要么已添加，删除掉该轨迹 **/
        MenuNode menuNode = menuNodeList.remove(0);
        /** 找到rootMenuNode子节点列表中存在的这个节点 **/
        MenuNode optionalMenuNode = rootMenuNode.getChildrenNode()
                .stream()
                .filter(menu -> menu.getNodeId().equals(menuNode.getNodeId()))
                .findFirst()
                .get();
        /** 如果子列表还有节点，则递归继续 **/
        assemblyMenuNode(optionalMenuNode, menuNodeList);
    }


}
